package com.satyam.smooth.domain.dao;

import com.satyam.smooth.dao.hibernate.GenericHibernateDAO;
import com.satyam.smooth.dao.hibernate.GenericSessionProvider;
import com.satyam.smooth.domain.Item;
import com.satyam.smooth.domain.ItemGroup;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author srt
 */
public class ItemHibernateDAO extends GenericHibernateDAO<Item, Long> {

    private Session session = null;

    public ItemHibernateDAO(GenericSessionProvider sessionProvider) {
        super(Item.class, sessionProvider);
        this.session = sessionProvider.getSession();
    }

    public List<Item> getItemsByItemGroup(ItemGroup itemGroup) {
        Criteria criteria = session.createCriteria(Item.class);
        criteria.add(Restrictions.eq("itemGroup", itemGroup));
        return criteria.list();
    }

    public Item getItemByCode(String code) {
        List<Item> itemList = session.createCriteria(Item.class)
                .add(Restrictions.eq("code", code))
                .list();
        if (CollectionUtils.isNotEmpty(itemList)) {
            return itemList.get(0);
        }
        return null;
    }

    public Item getItemByName(String name) {
        List<Item> itemList = session.createCriteria(Item.class)
                .add(Restrictions.eq("name", name))
                .list();
        if (CollectionUtils.isNotEmpty(itemList)) {
            return itemList.get(0);
        }
        return null;
    }

}
